package cn.lili.controller.user;

import cn.hutool.core.text.CharSequenceUtil;
import cn.lili.cache.Cache;
import cn.lili.common.aop.annotation.DemoSite;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.security.enums.SceneEnums;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.permission.entity.dos.Menu;
import cn.lili.modules.permission.entity.dos.RoleMenu;
import cn.lili.modules.permission.entity.dto.MenuSearchParams;
import cn.lili.modules.permission.entity.vo.MenuVO;
import cn.lili.modules.permission.integration.PermissionIntegrationService;
import cn.lili.modules.permission.service.MenuService;
import cn.lili.modules.permission.service.RoleMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;


/**
 * 菜单管理接口
 *
 * @author Chopper
 * @since 2020/11/20 12:07
 */
@Slf4j
@RestController
@Api(tags = "菜单管理接口")
@RequestMapping("/user/menu")
@RequiredArgsConstructor
public class MenuController {

    private final Cache cache;

    private final MenuService menuService;

    private final RoleMenuService roleMenuService;

    private final PermissionIntegrationService permissionIntegrationService;

    @ApiOperation(value = "搜索菜单")
    @GetMapping
    public ResultMessage<List<Menu>> searchPermissionList(MenuSearchParams searchParams) {
        return ResultUtil.data(menuService.searchList(searchParams));
    }

    @ApiOperation(value = "添加")
    @PostMapping
    @DemoSite
    public ResultMessage<Menu> add(Menu menu) {

        menuService.save(menu);
        return ResultUtil.data(menu);
    }

    @ApiImplicitParam(name = "id", value = "菜单ID", required = true, paramType = "path", dataType = "String")
    @ApiOperation(value = "编辑")
    @PutMapping(value = "/{id}")
    @DemoSite
    public ResultMessage<Menu> edit(@PathVariable String id, Menu menu) {
        menu.setId(id);
        menuService.saveOrUpdateMenu(menu);
        return ResultUtil.data(menu);
    }

    @ApiOperation(value = "批量删除")
    @DeleteMapping(value = "/{ids}")
    @DemoSite
    public ResultMessage<Menu> delByIds(@PathVariable List<String> ids) {
        permissionIntegrationService.deleteMenus(ids);
        return ResultUtil.success();
    }

    @ApiOperation(value = "获取所有菜单")
    @GetMapping("/tree")
    public ResultMessage<List<MenuVO>> getAllMenuList(String scene) {
        AuthUser currentUser = Objects.requireNonNull(UserContext.getCurrentUser());
        if (CharSequenceUtil.equalsAny(currentUser.getScene().name(), SceneEnums.MANAGER.name(), SceneEnums.STORE.name(), SceneEnums.SUPPLIER.name(), SceneEnums.URBAN_SUBSTATION.name())) {
            SceneEnums sceneEnums;
            if (CharSequenceUtil.isEmpty(scene)) {
                sceneEnums = currentUser.getScene();
            } else {
                sceneEnums = SceneEnums.valueOf(scene);
            }
            List<MenuVO> tree = menuService.tree(sceneEnums);
            return ResultUtil.data(tree);
        }
        throw new ServiceException(ResultCode.USER_AUTHORITY_ERROR);
    }

    @ApiOperation(value = "获取当前用户菜单")
    @GetMapping("/current")
    public ResultMessage<List<MenuVO>> memberMenu() {
        return ResultUtil.data(menuService.findUserTree());
    }


    @GetMapping(value = "/role/{roleId}")
    @ApiOperation(value = "查看某角色拥有到菜单")
    public ResultMessage<List<RoleMenu>> get(@PathVariable String roleId) {
        return ResultUtil.data(roleMenuService.findByRoleId(roleId));
    }
}
